import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/datacenter/user_center.dart';
import 'package:flutterlib/common/net/dao_result.dart';
import 'package:flutterlib/common/net/retrofit/interest_api.dart';
import 'package:flutterlib/common/net/retrofit/post_api.dart';
import 'package:flutterlib/common/style/quhui_style.dart';
import 'package:flutterlib/common/utils/navigator_utils.dart';
import 'package:flutterlib/common/utils/utils.dart';
import 'package:flutterlib/model/interest/interest_join.dart';
import 'package:flutterlib/pages/base/base_sliver_list.dart';
import 'package:flutterlib/pages/interest/viewmodel/interest_store.dart';
import 'package:flutterlib/widgets/bussiness/post/interest_list_item.dart';
import 'package:flutterlib/widgets/bussiness/post/post_simple_list_item.dart';
import 'package:flutterlib/widgets/pull/nested/simple_pull_load_manger.dart';
import 'package:flutterlib/widgets/pull/nested/simple_pull_load_page_widget.dart';
import 'package:fluttertoast/fluttertoast.dart';

class Page extends BaseSliverList {
  Page({Key key}) : super(key: key);

  @override
  PostMinePageState createState() {
    return PostMinePageState();
  }
}

///页面 KeepAlive ，同时支持 动画Ticker
class PostMinePageState extends BaseSliverListState<Page> {
  SimplePullLoadManger manger;

  InterestStore interestStore = new InterestStore();
  final SettingsStore settingStore =
      Provider.of<SettingsStore>(currentContext, listen: false);

  ///滑动监听
  final ScrollController scrollController = new ScrollController();

  @override
  getDataLogic(int page) async {
    print(page);

    var interests = await InterestApi.builder().getInterests();
    trueDebugPrint("flutterting" +
        "flutterting allinterests" +
        interests?.length.toString());
    return new DataResult(interests, true);
  }

  @override
  Widget renderItem(index, interest) {
    return Observer(builder: (_) {
      var interests = settingStore.interests;
      var isAdd =
          interests.filter((element) => element.id == interest.id).isNotEmpty;
      return InterestListItem(
        interest,
        onPressed: () {
          NavigatorUtils.gotoInterestDetail(context, interest.id);
        },
        addAble: true,
        isAdd: isAdd,
        onAddInterest: (interestId) {
          if (!isLogin) {
            NavigatorUtils.goLogin(context);
            return;
          }
          InterestJoin join = InterestJoin(interestId: interest.id);
          interestStore.joinInterest(join).then((value) {
            if (value) {
              settingStore.addInterest(getInterestFromId(interest.id));
            } else {
              ToastUtils.error("加入失败");
            }
          });
        },
      );
    });
  }

  @override
  List<Widget> renderHeader(BuildContext context, bool innerBoxIsScrolled) {
    return <Widget>[];
  }

  @override
  renderActions() {
    return <Widget>[
      InkWell(
        onTap: () {
          if (settingStore.interests.length > 0) {
            NavigatorUtils.gotoActionSelectPage(context);
          } else {
            ToastUtils.toast("请先加入兴趣");
          }
        },
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(
            "下一步",
            style: QuhuiConstant.smallText,
          ),
        ),
      )
    ];
  }

  @override
  Widget renderLeading() {
    return MyBackButton(
      color: Colors.black,
    );
  }

  @override
  Widget renderTitle() {
    return Text("选择兴趣", style: QuhuiConstant.normalTextBold);
  }

  @override
  bool enablePullDown() {
    return false;
  }

  @override
  bool enablePullUp() {
    return false;
  }
}
